package com.xingkeduo.business.order.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xingkeduo.dto.response.OrderAdminResponseDto;
import com.xingkeduo.dto.response.OrderRefundResponseDto;
import com.xingkeduo.pojo.enums.RefundType;
import com.xingkeduo.pojo.query.OrderMapper;
import com.xingkeduo.pojo.query.OrderRefundMapper;
import com.xingkeduo.pojo.table.Order;
import com.xingkeduo.pojo.table.OrderRefund;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * @author chi  2019-03-12 14:29
 **/
@Component
public class OrderRefundService {

    @Resource
    private OrderRefundMapper orderRefundMapper;
    @Resource
    private OrderMapper orderMapper;

    public Optional<OrderRefund> getRefundById(Serializable orderId) {
        OrderRefund orderRefund = orderRefundMapper.selectOne(new QueryWrapper<OrderRefund>().lambda().eq(OrderRefund::getOrderId, orderId));
        return Optional.ofNullable(orderRefund);
    }


    public OrderRefundResponseDto covertDto(OrderRefund orderRefund) {
        OrderRefundResponseDto orderRefundParamDto = new OrderRefundResponseDto();
        BeanUtils.copyProperties(orderRefund, orderRefundParamDto);

        if (RefundType.BACKGROUND.equals(orderRefund.getRefundType())) {
            orderRefundParamDto.setSponsorName("后台操作");
            orderRefundParamDto.setAuditingName("无");
        } else if (RefundType.USER.equals(orderRefund.getRefundType())) {
            orderRefundParamDto.setSponsorName("用户操作");
            orderRefundParamDto.setAuditingName("无");
        } else {
            if (StringUtils.isNotBlank(orderRefund.getSponsorId()) && orderRefund.getSponsorId().equals(orderRefund.getAuditingId())) {
                orderRefundParamDto.setSponsorName(orderRefundParamDto.getSponsorName() + "(经理直接申请)");
                orderRefundParamDto.setAuditingName(orderRefundParamDto.getSponsorName());
            }
        }


        return orderRefundParamDto;
    }

    public void updateOrInsert(OrderRefund orderRefund) {
        long id = orderRefund.getId();

        Order order = orderMapper.selectById(orderRefund.getOrderId());
        orderRefund.setOrderNo(order.getOrderNo())
                .setStoreId(order.getStoreId())
                .setAdminId(order.getAdminId());

        String a = "";
        if(StringUtils.isNotBlank(order.getOutOrderNo())){
            a = a + order.getOutOrderNo();
        }else if(StringUtils.isNotBlank(order.getAlipayTradeNo())){
            a = a + order.getAlipayTradeNo();
        }
        orderRefund.setRefundTradeNo(a);

        if(order.getServeStartTime()!=null){
            orderRefund.setServeStartTime(order.getServeStartTime());
        }
        if(order.getServeEndTime()!=null){
            orderRefund.setServeEndTime(order.getServeEndTime());
        }

        if (id > 0) {
            //修改
            orderRefundMapper.updateById(orderRefund);
        } else {
            //新增
            int insert = orderRefundMapper.insert(orderRefund);
        }
    }


    public OrderAdminResponseDto peekOrderAdminResponseDto(OrderAdminResponseDto order) {
        Optional<OrderRefund> orderRefundOptional = getRefundById(String.valueOf(order.getId()));
        orderRefundOptional.ifPresent(orderRefund -> {
            order.setRefundReason(orderRefund.getRefundReason());
        });
        return order;
    }

    /**
     * 统计退款订单数
     * @param storeId
     * @param startTime
     * @param endTime
     * @return
     */
    public Integer serviceRefund(String storeId,LocalDateTime startTime,LocalDateTime endTime) {
        return orderRefundMapper.selectCount(new QueryWrapper<OrderRefund>().lambda()
                .between(OrderRefund::getRefundTime,startTime,endTime)
                .between(OrderRefund::getServeStartTime,startTime,endTime)
                .eq(OrderRefund::getStoreId,storeId));
    }

    /**
     * 退款订单列表
     * @param storeId
     * @param startTime
     * @param endTime
     * @return
     */
    public List<OrderRefund> serviceRefundList(String storeId,LocalDateTime startTime,LocalDateTime endTime) {
        return orderRefundMapper.selectList(new QueryWrapper<OrderRefund>().lambda()
                .between(OrderRefund::getRefundTime,startTime,endTime)
                .eq(OrderRefund::getStoreId,storeId));
    }

    /**
     * 查询退款数
     * @param storeId
     * @param adminId
     * @param startTime
     * @param endTime
     * @return
     */
    public Integer countRefundByAdminId(String storeId,String adminId, LocalDateTime startTime, LocalDateTime endTime) {
        return orderRefundMapper.selectCount(new QueryWrapper<OrderRefund>().lambda()
                .between(OrderRefund::getRefundTime,startTime,endTime)
                .between(OrderRefund::getServeStartTime,startTime,endTime)
                .eq(OrderRefund::getAdminId,adminId)
                .eq(OrderRefund::getStoreId,storeId));
    }
}
